package com.telsoft.managedbean.function;

import com.telsoft.business.ComponentBusiness;
import com.telsoft.business.GroupBusiness;
import com.telsoft.business.LibraryBusiness;
import com.telsoft.entity.ComponentEntity;
import com.telsoft.entity.GroupEntity;
import com.telsoft.entity.LibraryEntity;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

@SuppressWarnings("serial")
@ManagedBean(name = "leftBean")
@RequestScoped
public class LeftBean implements Serializable {
    //var

    private TreeNode root;
    private List<LibraryEntity> librarys;
    private TreeNode comSelected;
    private Func_downloadleft funcDownload;
    //getter

    public TreeNode getRoot() {
        return root;
    }

    public List<LibraryEntity> getLib() {
        return librarys;
    }

    public TreeNode getComSelected() {
        return comSelected;
    }

    //setter
    public void setRoot(TreeNode root) {
        this.root = root;
    }

    public void setLib(List<LibraryEntity> lib) {
        this.librarys = lib;
    }

    public void setComSelected(TreeNode comSelected) {
        this.comSelected = comSelected;
    }

    //contructor
    public LeftBean() {
        root = new DefaultTreeNode(root, null);
        FacesContext context = FacesContext.getCurrentInstance();
        funcDownload = (Func_downloadleft) context.getApplication().evaluateExpressionGet(context, "#{func_downloadleft}", Func_downloadleft.class);
        try {
            librarys = LibraryBusiness.getLibrary();
            List<GroupEntity> grComs = GroupBusiness.getGRComponent();
            List<ComponentEntity> coms = ComponentBusiness.getComponent();
            for (LibraryEntity item : librarys) {
                TreeNode lib = new DefaultTreeNode(item, root);
                for (GroupEntity itemGr : grComs) {
                    TreeNode grC = null;
                    if (itemGr.getLibID() == item.getLibID()) {
                        grC = new DefaultTreeNode(itemGr, lib);
                    }
                    for (ComponentEntity itemC : coms) {
                        if (itemC.getGrComID() == itemGr.getGrComID()) {
                            TreeNode c = new DefaultTreeNode(itemC, grC);
                        }
                    }
                }
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
        }


    }

    public void onNodeSelect(NodeSelectEvent event) throws SQLException {
        this.comSelected =  event.getTreeNode();
        FacesContext context = FacesContext.getCurrentInstance();
        RightCenterBean rightBean = (RightCenterBean)context.getApplication().evaluateExpressionGet(context, "#{rightCenterBean}", RightCenterBean.class);
        rightBean.initRightCenter(comSelected);
    }

    public Func_downloadleft getFuncDownload() {
        return funcDownload;
    }

    public void setFuncDownload(Func_downloadleft funcDownload) {
        this.funcDownload = funcDownload;
    }
    
}
